<template>
	<view class="comp-b-container">
		<view class="title" style="font-size:50rpx;">
			子组件B
		</view>
		<view class="b-box">
			CompA组件传进来的值：
			<text class="intent-text-box">{{result}}</text>
		</view>
		<view class="b-box" style="margin: 10rpx;">
			<text>回传值：</text>
			<input type="text" v-model="callbackValue"
				style="color: yellow; background-color: #666; padding: 10rpx; border-radius: 5rpx; border: 1rpx solid #999;"
				placeholder="输入回传值" />
		</view>
		<button @click="sendOutside()" size="mini">回传</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				result: 'Hello',
				callbackValue: '10086'
			}
		},
		mounted() {
			uni.$on('sendToCompB', (data) => {
				this.result = data || 'Hello';
				uni.showToast({
					title: '收到数据',
					icon: 'success'
				});
			});
		},
		beforeDestroy() {
			uni.$off('sendToCompB');
		},
		methods: {
			sendOutside() {
				if (this.callbackValue) {
					this.$emit('callBackFun', this.callbackValue);
					uni.showToast({
						title: '回传成功',
						icon: 'success'
					});
				} else {
					uni.showToast({
						title: '请输入回传值',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style scoped>
	.comp-b-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 30rpx;
		margin: 20rpx;
		background-color: #f6ffed !important;
		/* 浅绿色背景，使用!important确保优先级 */
		border-radius: 15rpx;
		border: 2rpx solid #b7eb8f;
	}

	.title {
		font-size: 50rpx;
		margin-bottom: 20rpx;
	}

	.b-box {
		margin: 10rpx 0;
		padding: 20rpx;
		border: 1px solid #ccc;
		border-radius: 10rpx;
		width: 100%;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		background-color: white;
	}

	.intent-text-box {
		font-weight: bold;
		color: red;
		margin-left: 10rpx;
	}
</style>